﻿using org.se.fs.presenter.Library;

namespace org.se.fs.presenter.View.Loan
{
    public class RegularLoan : Template.SubMenu 
    {
        private readonly IView.Loan.IRegularLoan _view;
        private readonly Domain.RegularLoan _domain;
        private readonly ITask.Loan.IRegularLoan _task;


        public RegularLoan(IView.Loan.IRegularLoan view) : this(view, new Task.Context.RegularLoan()) { }

        public RegularLoan(IView.Loan.IRegularLoan view, ITask.Loan.IRegularLoan task)
        {
            _view = view;
            _task = task;
            _domain = new Domain.RegularLoan();
            _domain.PropertyChanged += DomainPropertyChanged;
        }

        public override void Initialize()
        {
            _task.OnInsert += TaskOnInsert;
            _task.OnUpdate += TaskOnUpdate;
        }

        protected override void TaskOnInsert(object source, FormEvent e)
        {
            //_view.ButtonAddEnable(true);
            //TriggerModify(new FormEvent(e.IsSuccess) { Exception = e.Exception, Value = e.Value });
        }

        protected override void TaskOnUpdate(object source, FormEvent e)
        {
            //_view.ButtonUpdateEnable(true);
            //TriggerModify(new FormEvent(e.IsSuccess) {Exception = e.Exception, Value = e.Value});
        }

        public override void Add()
        {
            //if (!Validate())
            //    return;

            //_view.ButtonAddEnable(false);
            //var a = Data.NewRow();
            //a["name"] = _view.FullName;
            //a["code"] = _view.Code;
            //a["address"] = _view.Address;
            //a["email"] = _view.Email;
            //a["fax_num"] = _view.Fax;
            //a["mobile_num"] = _view.Mobile;
            //a["tel_num"] = _view.Tel;
            //a["user_create_id"] = Convert.ToUInt64(Config.User.Rows[0]["id"].ToString());
            //a["last_name"] = Config.User.Rows[0]["last_name"].ToString();
            //a["first_name"] = Config.User.Rows[0]["first_name"].ToString();
            //a["middle_name"] = Config.User.Rows[0]["middle_name"].ToString();
            //a["user_name"] = Config.User.Rows[0]["user_name"].ToString();
            //Data.Rows.Add(a);
            //_task.Insert(Data);
        }

        public override void Modify()
        {
            //if(!Validate())
            //    return;

            //_view.ButtonUpdateEnable(false);
            //Data.Rows[0]["name"] = _view.FullName;
            //Data.Rows[0]["code"] = _view.Code;
            //Data.Rows[0]["address"] = _view.Address;
            //Data.Rows[0]["email"] = _view.Email;
            //Data.Rows[0]["fax_num"] = _view.Fax;
            //Data.Rows[0]["mobile_num"] = _view.Mobile;
            //Data.Rows[0]["tel_num"] = _view.Tel;
            //Data.Rows[0]["user_create_id"] = Convert.ToUInt64(Config.User.Rows[0]["id"].ToString());
            //Data.Rows[0]["last_name"] = Config.User.Rows[0]["last_name"].ToString();
            //Data.Rows[0]["first_name"] = Config.User.Rows[0]["first_name"].ToString();
            //Data.Rows[0]["middle_name"] = Config.User.Rows[0]["middle_name"].ToString();
            //Data.Rows[0]["user_name"] = Config.User.Rows[0]["user_name"].ToString();
            //_task.Update(Data);
        }

        protected override bool Validate()
        {
            //if (string.IsNullOrEmpty(_view.FullName.Trim()))
            //{
            //    TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty name.") });
            //    _view.FocusName();
            //    return false;
            //}

            //if (string.IsNullOrEmpty(_view.Code.Trim()))
            //{
            //    TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty code.") });
            //    _view.FocusCode();
            //    return false;
            //}

            //if (string.IsNullOrEmpty(_view.Address.Trim()))
            //{
            //    TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty address.") });
            //    _view.FocusAddress();
            //    return false;
            //}

            //TriggerValidate(new FormEvent(true));
            return true;
        }

        protected override void SetData()
        {
            if (Data.Rows.Count.Equals(0))
                return;

            //_view.FullName = Data.Rows[0]["name"].ToString();
            //_view.Code = Data.Rows[0]["code"].ToString();
            //_view.Address = Data.Rows[0]["address"].ToString();
            //_view.Fax = Data.Rows[0]["fax_num"].ToString();
            //_view.Email = Data.Rows[0]["email"].ToString();
            //_view.Mobile = Data.Rows[0]["mobile_num"].ToString();
            //_view.Tel = Data.Rows[0]["tel_num"].ToString();
        }
    }
}
